package com.gxitsky.channel;

import org.yaml.snakeyaml.Yaml;

import java.io.*;

/**
 * @author gxing
 * @desc 统一响应码
 * @date 2022/1/22
 */
public class YamlHelper {

    private YamlHelper() {
    }

    private static final Yaml YAML = new Yaml();

    /**
     * @param file  文件路径
     * @param clazz 对象Clazz
     * @desc YAML 转对象
     * @author gxing
     * @date 2022/1/24 10:58
     */
    public static <T> T parseYaml(File file, Class<T> clazz) {
        try {
            return parseYaml(new FileInputStream(file), clazz);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * @param inputStream 输入流
     * @param clazz       对象Clazz
     * @desc YAML 转对象
     * @author gxing
     * @date 2022/1/24 10:58
     */
    public static <T> T parseYaml(InputStream inputStream, Class<T> clazz) {
        return YAML.loadAs(inputStream, clazz);
    }


    public static <T> T parseYaml(String input, Class<T> clazz) {
        return YAML.loadAs(input, clazz);
    }


    public static <T> T parseYaml(Reader io, Class<T> clazz) {
        return YAML.loadAs(io, clazz);
    }

}
